<?php
if(!defined('IN_SITE')) exit('Access Denied');
/*
	 * 对比模板文件与缓存文件的hash值
	 *
	 * @param unknown_type $file
	 * @return unknown
*/

function match_hash($objfile,$tplfile){
	$read_hash = file_exists($objfile)?filemtime($objfile):0;

	$html_hash = file_exists($tplfile)?filemtime($tplfile):die('模板文件'.$tplfile.'不存在,或没有操作权限');//获取模板文件的最后修改时间

	if($read_hash>$html_hash)
	{
		return true;
	}
}


 /**
	 * 读取文件内容
	 *
	 * @param unknown_type $path
	 * @return unknown
 */
function file_read($path,$length=0){
	$fp = @fopen($path,"rb");
	if($length==0) {
		$contents = @fread($fp,filesize($path));
	} else {
		$contents = @fread($fp,$length);
	}
	if(!empty($fp))
		fclose($fp);
	return $contents;
}


/*
	 *
	 * 写入文件内容
	 *
	 * @param unknown_type $path
	 * @param unknown_type $puts
*/
function file_write($path,$puts) {
	if(!file_test($path,"w+")) {
		$fp = @fopen($path,"w+");
		flock($fp, 2);
		@fwrite($fp,$puts);
		fclose($fp);
	}
}

/*
 *
 * 检查文件是否存在并且有读取权限
 *
 * @param unknown_type $path
*/

function file_test($path,$method) {
	if(!file_exists($path) || !fopen($path,$method)) {
		echo "模板文件不存在,或没有操作权限";
		return false;
	}
}




function transamp($str) {
		//$str = str_replace('&', '&amp;', $str);
		$str = str_replace('&amp;amp;', '&amp;', $str);
		$str = str_replace('\"', '"', $str);
		return $str;
	}

function addquote($var) {
	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

function stripvtags($expr, $statement) {
	$expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
	$statement = str_replace("\\\"", "\"", $statement);
	return $expr.$statement;
}

function stripscriptamp($s)
{
	$s = str_replace('&amp;', '&', $s);
	return "<script src=\"$s\" type=\"text/javascript\"></script>";
}

function languagevar($var) {
    
	
   
	if(isset($GLOBALS['lang'][$var])) {
			return $GLOBALS['lang'][$var];
	} else {
			return "!$var!";
	}
}


//模板编译
function template($file, $templateid='tp') {
	global $_TEMPLATESDIR,$_CACHEDIR;
	
	//模板代码嵌套层次
	$nest    = 5;
	
	$tplfile = $_TEMPLATESDIR."/$file.htm";
	
	$objfile = $_CACHEDIR."/{$templateid}_$file.tpl.php";
	if( match_hash($objfile,$tplfile) ) {
	   return $objfile;

	} else {
		$template = file_read($tplfile, filesize($tplfile));
		
		$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
		$const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";

		$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
		$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
		$template = preg_replace("/\{lang\s+(.+?)\}/ies", "languagevar('\\1')", $template);
		$template = str_replace("{LF}", "<?=\"\\n\"?>", $template);

		$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
		$template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
		$template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1?>')", $template);

		$template = "<?php if(!defined('IN_SITE')) exit('Access Denied'); ?>\n$template";
		$template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is", "\n<?php include template('\\1'); ?>\n", $template);
		$template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<?php \\1 ?>\n','')", $template);
		$template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<?php echo \\1; ?>\n','')", $template);
		$template = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<?php } elseif(\\1) { ?>\n','')", $template);
		$template = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n<?php } else { ?>\n", $template);

		for($i = 0; $i < $nest; $i++) {
			$template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('\n<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\n\\3\n<?php } } ?>\n')", $template);
			$template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('\n<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','\n\\4\n<?php } } ?>\n')", $template);
			$template = preg_replace("/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies", "stripvtags('\n<?php if(\\1) { ?>','\n\\2\n<? } ?>\n')", $template);
		}

		$template = preg_replace("/\{$const_regexp\}/s", "<?=\\1?>", $template);
		$template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
		
		!file_exists($_CACHEDIR) && mkdir($_CACHEDIR);
		if(!@$fp = fopen($objfile, 'w')) {
			die("Directory $_CACHEDIR not found or have no access!");
		}

		$template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "transamp('\\0')", $template);
		$template = preg_replace("/\<script[^\>]*?src=\"(.+?)\".*?\>\s*\<\/script\>/ise", "stripscriptamp('\\1')", $template);
		$head_hash = "<?php #CreateTime=".date("Y-m-d H:i:s")."?>";
		file_write($objfile,$head_hash.$template);
		return $objfile;
	}
}
?>
